package com.bdqn.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.blog.entity.Rank;
import com.bdqn.blog.service.impl.RankServiceImpl;
import com.bdqn.blog.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 评价 前端控制器
 * </p>
 *
 * @author 阿桐幕
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/blog/rank")
public class RankController {

    @Autowired
    private RankServiceImpl rankService;

    //获取某歌单的评分
    @GetMapping("/getRank/{id}")
    public Result getRank(@PathVariable Integer id){
        int rank = rankService.getRank(id);
        return  Result.success(rank);
    }

    //给某歌单评分
    @PostMapping("/ModifyRank")
    public Result ModifyRank(@RequestBody  Rank rank){
        QueryWrapper<Rank> wrapper = new QueryWrapper<>();
        wrapper
                .eq("song_list_id",rank.getSongListId())
                .eq("consumer_id",rank.getConsumerId());
        Rank rank1 = rankService.getOne(wrapper);
        if (rank1 != null){
            return Result.fail("您已经评价过了,请勿重复评价！");
        }else {
            boolean save = rankService.save(rank);
            if (save){
                return Result.success("评价成功！");
            }else {
                return  Result.fail("由于某种不知名原因,导致评价失败！");
            }
        }
    }
}

